//
//  ZFBHomeTopView.m
//  01-ZFB
//
//  Created by My Mac on 2017/6/1.
//  Copyright © 2017年 MyMac. All rights reserved.
//

#import "ZFBHomeTopView.h"
#import "NSAttributedString+Additon.h"
#import "ZFBHomeTopButton.h"

@implementation ZFBHomeTopView

-(void)awakeFromNib
{
    [super awakeFromNib];
    [self setUpUI];
}

//重写初始化方法并添加子控件
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
    }
    return self;
}

-(void)setUpUI
{
    //扫一扫
    UIButton *scanBtn = [self makeHomeTopViewWithButtonImageName:@"home_scan" buttonTitle:@"扫一扫" buttonType:ZFBHomeTopViewButtonTypeScan];
    //付款
    UIButton *payBtn =[self makeHomeTopViewWithButtonImageName:@"home_pay" buttonTitle:@"付款" buttonType:ZFBHomeTopViewButtonTypePay];
    //卡券
    UIButton *cardBtn =[self makeHomeTopViewWithButtonImageName:@"home_card" buttonTitle:@"卡券" buttonType:ZFBHomeTopViewButtonTypeCard];
    //咻一咻
    UIButton *xiuBtn = [self makeHomeTopViewWithButtonImageName:@"home_xiu" buttonTitle:@"咻一咻" buttonType:ZFBHomeTopViewButtonTypeXiu];
    //设置等宽等高的布局约束
    [self.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
    }];
    [scanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
    }];
    [payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(scanBtn.mas_right).offset(0);
        make.width.equalTo(scanBtn.mas_width).offset(0);
    }];
    [cardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(payBtn.mas_right).offset(0);
        make.width.equalTo(payBtn.mas_width).offset(0);
    }];
    [xiuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cardBtn.mas_right).offset(0);
        make.width.equalTo(cardBtn.mas_width).offset(0);
        make.right.offset(0);
    }];
    
}

-(UIButton *)makeHomeTopViewWithButtonImageName:(NSString *)imageName buttonTitle:(NSString *)buttonTitle buttonType:(ZFBHomeTopViewButtonType)type
{
    ZFBHomeTopButton *button = [ZFBHomeTopButton buttonWithType:UIButtonTypeSystem];
    [button setImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [button setTitle:buttonTitle forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    button.tag = type;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    return button;
}


#pragma mark - 方法二：使用第三方框架
//-(UIButton *)makeHomeTopViewWithButtonImageName:(NSString *)imageName buttonTitle:(NSString *)buttonTitle
//{
//    NSLog(@"%d",numTag);
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    NSAttributedString *attrStr = [NSAttributedString imageTextWithImage:[UIImage imageNamed:imageName] imageWH:35 title:buttonTitle fontSize:15 titleColor:[UIColor whiteColor] spacing:8];
//    button.tag = numTag;
//    //设置按钮换行
//    button.titleLabel.numberOfLines = 0;
//    //设置对齐方式
//    button.titleLabel.textAlignment = NSTextAlignmentCenter;
//    [button setAttributedTitle:attrStr forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:button];
//    return button;
//}

-(void)buttonClick:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(homeTopView:topButtonClickType:title:)]) {
        [self.delegate homeTopView:self topButtonClickType:button.tag title:button.titleLabel.text];
    }
}

@end
